<template>
  <div>
    <el-form :inline="true" :model="queryForm" class="demo-form-inline">
      <el-form-item label="员工管理">
        <el-input v-model="queryForm.kw" placeholder="姓名" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button type="success" @click="handleAdd">新增</el-button>
        
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="部门" align="center" prop="dept.deptName"/>
      <el-table-column label="员工号" align="center" prop="employeeNo"/>
      <el-table-column label="职务" align="center" prop="position">
        <template #default="scope">
            <div v-if="scope.row.position==1">员工</div>
            <div v-if="scope.row.position==2">部长</div>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="realName">
        <template #default="scope">
            <div>{{scope.row.realName}}</div>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="gender"/>
      <!-- <el-table-column label="所在地区" align="center" prop="location" /> -->
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="工资" align="center" prop="salary"/>
      <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
              <el-tag type="success" v-if="scope.row.employStatus==1">在职</el-tag>
              <el-tag type="info" v-if="scope.row.employStatus==-1">已离职</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="220">
        <template #default="scope">
          <div v-if="scope.row.employStatus==1">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row.employeeId)">修改</el-button>
            <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row.employeeId, -1)">离职注销</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background layout="prev, pager, next" v-model:page-size="queryForm.pageSize" v-model:current-page="queryForm.pageNo" :total="total" @current-change="getList" />

    <!-- 添加或修改用户对话框 -->
    <el-dialog v-model="open" :title="title" width="800px" append-to-body>
      <el-form label-width="120px" :model="form" :rules="rules" ref="formRef">
        <el-form-item label="所属部门" prop="deptId">
          <el-select v-model="form.deptId" placeholder="请选择部门">
            <el-option
              v-for="item in deptList"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="员工号" prop="employeeNo">
          <el-input
            v-model="form.employeeNo"
            auto-complete="off"
            placeholder=""
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="realName">
          <el-input
            v-model="form.realName"
            auto-complete="off"
            placeholder=""
          ></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio value="男">男</el-radio>
            <el-radio value="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input
            v-model="form.mobile"
            auto-complete="off"
            placeholder=""
          ></el-input>
        </el-form-item>
        <el-form-item label="默认密码" prop="pwd">
          <el-input
            v-model="form.pwd"
            auto-complete="off"
            placeholder=""
          ></el-input>
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-radio-group v-model="form.position">
            <el-radio :value="1">员工</el-radio>
            <el-radio :value="2">部长</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="工资" prop="salary">
          <el-input
            v-model="form.salary"
            auto-complete="off"
            placeholder=""
            type="number"
            step="0.1"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>
<script setup lang="ts">
import { ref, reactive, nextTick, toRef } from "vue";
import baseService from "@/service/baseService";
import { ElMessage, ElMessageBox } from "element-plus";
import { timestampToDayHM, timestampToHM, formatAmount } from "@/utils/utils"

// 查询参数
const queryForm = reactive({
  kw: "",
  role: 1,
  pageNo: 1,
  pageSize: 10
});
// 列表内容数量
const total = ref(0);
// 列表是否加载
const loading = ref(true);
// 列表返回值
const list = ref<any[]>([]);
const deptList = ref<any[]>([]);

// 表单实例
const formRef = ref();
// 是否打开弹出框
const open = ref(false);
// 弹出框标题
const title = ref("");
// 提交表单数据
let form = toRef(
  reactive({
    employeeId: "",
    employeeNo: "",
    deptId: "",
    realName: "",
    pwd: "",
    gender: "男",
    birthday: "",
    idNo: "",
    mobile: "",
    position: 1,
    salary: "",
    employStatus: 1
  })
);
// 表单验证
const rules = ref({
  phone: [{ required: true, message: "手机号不能为空", trigger: "blur" }],
});

/**
 * 查询列表
 */
const getList = () => {
  loading.value = true;
  baseService
    .post("/admin/user/list", queryForm)
    .then((res) => {
      loading.value = false;
      if (res.code === 200) {
        list.value = res.data.records;
        total.value = res.data.totalrecord;
      } else {
        list.value = [];
      }
    }).catch(() => {
      loading.value = false;
    });
};

/**
 * 搜索按钮操作
 */
function handleQuery() {
  queryForm.pageNo = 1;
  getList();
}

/**
 * 表单重置
 */
function reset() {
  form.value = {
    employeeId: "",
    employeeNo: "",
    deptId: "",
    realName: "",
    pwd: "",
    gender: "男",
    birthday: "",
    idNo: "",
    mobile: "",
    position: 1,
    salary: "",
    employStatus: 1
  };

  nextTick(() => {
    formRef.value?.resetFields();
  });
}

/**
 * 新增按钮操作
 */
function handleAdd() {
  reset();
  baseService.get(`/dept/getAll`).then((res) => {
    if(res.code==200){
      deptList.value = res.data
    }
  })
  open.value = true;
  title.value = "添加";
}

/**
 * 提交按钮
 */
function submitForm() {
  formRef.value.validate((valid: boolean) => {
    if (!valid) return;
    baseService.post(`/admin/user/save`, form.value).then((res) => {
      if (res.code === 200) {
        ElMessage.success(res.msg);
        open.value = false;
        getList();
      } else {
        ElMessage.error(res.msg);
      }
    });
  });
}

/**
 * 删除按钮操作
 * @param id 主键
 */
function handleDelete(id: any, status: number) {
  let msg = "确定要注销该用户吗?";
  let req = {
    employeeId: id,
    status: status
  }
  ElMessageBox.confirm(msg, "提示").then(() => {
    baseService.post(`/admin/user/update2`, req).then((res) => {
      if (res.code === 200) {
        ElMessage.success(res.msg);
        getList();
      } else {
        ElMessage.error(res.msg);
      }
    });
  });
}

/**
 * 修改按钮操作
 * @param id 主键
 */
 function handleUpdate(id: string) {
  open.value = true;
  title.value = "修改";
  reset();
  baseService.get(`/admin/user/info/${id}`).then((res) => {
    if (res.code === 200) {
      form.value = res.data;
    }
  });
}

getList();
</script>

<style scoped></style>
